//client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define SERVPORT 3333
#define MAXDATASIZE 5120
const char *sock_str = "Code updates for warnings reported by Coverity scans Testing and warning fixes for FreeBSD on PowerPC Updates and sfdnkldjkljals";
char               buf[ MAXDATASIZE ];

int main(int argc, char *argv[])
{
	int sockfd,  sendbytes, recvbytes;
	char buf[MAXDATASIZE];
	struct hostent *host;
	struct sockaddr_in serv_addr;
	printf("MSG_CMSG_CLOEXEC=%x MSG_DONTWAIT=%x MSG_ERRQUEUE=%x\n",MSG_CMSG_CLOEXEC, MSG_DONTWAIT, MSG_ERRQUEUE);
	if(argc < 2)
	{
		fprintf(stderr, "Please enter the server's hostname!\n");
		exit(1);
	}
	//the function of address parasing function
	if((host = gethostbyname(argv[1])) == NULL)
	{
		perror("gethostbyname");
		exit(1);
	}
	//creating socket
	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket");
		exit(1);
	}
	//setup parameters about sockaddr_in struct
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERVPORT);
	serv_addr.sin_addr.s_addr = inet_addr(argv[1]); //((struct in_addr *)host->h_addr)->s_addr; //
	bzero(&(serv_addr.sin_zero), 8);
	//connect the server by invoking the function connect
	if(connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1)
	{
		perror("connect");
		exit(1);
	}
	while (1) {
		//send the message to server
		if((sendbytes = send(sockfd, sock_str, strlen(sock_str), 0)) == -1) {
			perror("send");
			exit(1);
		} else {
			printf("send : %s\r", sock_str);
		}
		usleep(1000*200);
		// if ((recvbytes=recv(sockfd, buf, sendbytes, 0)) == -1) {
		// 	perror("recv");
		// 	exit(1);
		// }
		// buf[recvbytes] = 0;
		// printf("recv from server:%s\n", buf);
	}
	close(sockfd);
}

